/*
 * Copyright (c) 2024 vitasystems GmbH
 *
 * This file is part of project EHRbase Migration Tool
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ehrbase.migration.importer.v4.jooq.pg.tables;

import java.time.OffsetDateTime;
import java.util.function.Function;
import org.ehrbase.migration.importer.v4.jooq.pg.Keys;
import org.ehrbase.migration.importer.v4.jooq.pg.tables.records.StoredQueryRecord;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.Function6;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.Records;
import org.jooq.Row6;
import org.jooq.Schema;
import org.jooq.SelectField;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.TableOptions;
import org.jooq.UniqueKey;
import org.jooq.impl.DSL;
import org.jooq.impl.SQLDataType;
import org.jooq.impl.TableImpl;

/**
 * This class is generated by jOOQ.
 */
@SuppressWarnings({"all", "unchecked", "rawtypes", "this-escape"})
public class StoredQuery extends TableImpl<StoredQueryRecord> {

    private static final long serialVersionUID = 1L;

    /**
     * The reference instance of <code>ehr.stored_query</code>
     */
    public static final StoredQuery STORED_QUERY = new StoredQuery();

    /**
     * The class holding records for this type
     */
    @Override
    public Class<StoredQueryRecord> getRecordType() {
        return StoredQueryRecord.class;
    }

    /**
     * The column <code>ehr.stored_query.reverse_domain_name</code>.
     */
    public final TableField<StoredQueryRecord, String> REVERSE_DOMAIN_NAME =
            createField(DSL.name("reverse_domain_name"), SQLDataType.VARCHAR.nullable(false), this, "");

    /**
     * The column <code>ehr.stored_query.semantic_id</code>.
     */
    public final TableField<StoredQueryRecord, String> SEMANTIC_ID =
            createField(DSL.name("semantic_id"), SQLDataType.VARCHAR.nullable(false), this, "");

    /**
     * The column <code>ehr.stored_query.semver</code>.
     */
    public final TableField<StoredQueryRecord, String> SEMVER = createField(
            DSL.name("semver"),
            SQLDataType.VARCHAR
                    .nullable(false)
                    .defaultValue(DSL.field(DSL.raw("'0.0.0'::character varying"), SQLDataType.VARCHAR)),
            this,
            "");

    /**
     * The column <code>ehr.stored_query.query_text</code>.
     */
    public final TableField<StoredQueryRecord, String> QUERY_TEXT =
            createField(DSL.name("query_text"), SQLDataType.VARCHAR.nullable(false), this, "");

    /**
     * The column <code>ehr.stored_query.type</code>.
     */
    public final TableField<StoredQueryRecord, String> TYPE = createField(
            DSL.name("type"),
            SQLDataType.VARCHAR.defaultValue(DSL.field(DSL.raw("'AQL'::character varying"), SQLDataType.VARCHAR)),
            this,
            "");

    /**
     * The column <code>ehr.stored_query.creation_date</code>.
     */
    public final TableField<StoredQueryRecord, OffsetDateTime> CREATION_DATE = createField(
            DSL.name("creation_date"), SQLDataType.TIMESTAMPWITHTIMEZONE(6).nullable(false), this, "");

    private StoredQuery(Name alias, Table<StoredQueryRecord> aliased) {
        this(alias, aliased, null);
    }

    private StoredQuery(Name alias, Table<StoredQueryRecord> aliased, Field<?>[] parameters) {
        super(alias, null, aliased, parameters, DSL.comment(""), TableOptions.table());
    }

    /**
     * Create an aliased <code>ehr.stored_query</code> table reference
     */
    public StoredQuery(String alias) {
        this(DSL.name(alias), STORED_QUERY);
    }

    /**
     * Create an aliased <code>ehr.stored_query</code> table reference
     */
    public StoredQuery(Name alias) {
        this(alias, STORED_QUERY);
    }

    /**
     * Create a <code>ehr.stored_query</code> table reference
     */
    public StoredQuery() {
        this(DSL.name("stored_query"), null);
    }

    public <O extends Record> StoredQuery(Table<O> child, ForeignKey<O, StoredQueryRecord> key) {
        super(child, key, STORED_QUERY);
    }

    @Override
    public Schema getSchema() {
        return aliased() ? null : org.ehrbase.migration.importer.v4.jooq.pg.Ehr.EHR;
    }

    @Override
    public UniqueKey<StoredQueryRecord> getPrimaryKey() {
        return Keys.STORED_QUERY_PKEY;
    }

    @Override
    public StoredQuery as(String alias) {
        return new StoredQuery(DSL.name(alias), this);
    }

    @Override
    public StoredQuery as(Name alias) {
        return new StoredQuery(alias, this);
    }

    @Override
    public StoredQuery as(Table<?> alias) {
        return new StoredQuery(alias.getQualifiedName(), this);
    }

    /**
     * Rename this table
     */
    @Override
    public StoredQuery rename(String name) {
        return new StoredQuery(DSL.name(name), null);
    }

    /**
     * Rename this table
     */
    @Override
    public StoredQuery rename(Name name) {
        return new StoredQuery(name, null);
    }

    /**
     * Rename this table
     */
    @Override
    public StoredQuery rename(Table<?> name) {
        return new StoredQuery(name.getQualifiedName(), null);
    }

    // -------------------------------------------------------------------------
    // Row6 type methods
    // -------------------------------------------------------------------------

    @Override
    public Row6<String, String, String, String, String, OffsetDateTime> fieldsRow() {
        return (Row6) super.fieldsRow();
    }

    /**
     * Convenience mapping calling {@link SelectField#convertFrom(Function)}.
     */
    public <U> SelectField<U> mapping(
            Function6<
                            ? super String,
                            ? super String,
                            ? super String,
                            ? super String,
                            ? super String,
                            ? super OffsetDateTime,
                            ? extends U>
                    from) {
        return convertFrom(Records.mapping(from));
    }

    /**
     * Convenience mapping calling {@link SelectField#convertFrom(Class,
     * Function)}.
     */
    public <U> SelectField<U> mapping(
            Class<U> toType,
            Function6<
                            ? super String,
                            ? super String,
                            ? super String,
                            ? super String,
                            ? super String,
                            ? super OffsetDateTime,
                            ? extends U>
                    from) {
        return convertFrom(toType, Records.mapping(from));
    }
}
